#include "physic.h"


static int collisionm(s_monster *m)
{
  return ((m->coord.x + m->size.x > g_player_1->coord.x) &&
	  (m->coord.x < g_player_1->coord.x + g_player_1->size.x)
	  && (m->coord.y + m->size.y > g_player_1->coord.y) &&
	  g_player_1->coord.y + g_player_1->size.y > m->coord.y);
}

static int collisionw(s_weapon *w)
{
  return ((w->coord.x + w->size.x > g_player_1->coord.x) &&
          (w->coord.x < g_player_1->coord.x + g_player_1->size.x)
          && (w->coord.y + w->size.y > g_player_1->coord.y) &&
          g_player_1->coord.y + g_player_1->size.y > w->coord.y);
}

static int collisionp(s_monster *m, s_weapon *w)
{
  return ((w->coord.x + w->size.x > m->coord.x) &&
          (w->coord.x < m->coord.x + m->size.x)
          && (w->coord.y + w->size.y > m->coord.y) &&
          m->coord.y + m->size.y > w->coord.y);
}

void monster_col(s_wlist *wlist)
{
  s_wlist       *wtmp = wlist;
  //  s_wlist       *dwlist = NULL;
  s_weapon      *w = NULL;
  s_mlist       *mtmp = g_mlist;
  s_mlist       *dmlist = NULL;
  s_monster     *m = NULL;

  while (wtmp != NULL)
  {
    w = wtmp->weapon;
    while (mtmp != NULL)
    {
      m = mtmp->monster;
      if (collisionp(m, w))
      {
	//	delete_weapon(g_wlist, wtmp);
	dmlist = mtmp;
	if (mtmp != NULL)
	  mtmp = mtmp->next;
	delete_monster(dmlist);
	break;
      }
      else
	mtmp = mtmp->next;
    }
    wtmp = wtmp->next;
  }
}
void player_col(s_wlist *wlist)
{
  s_wlist	*wtmp = wlist;
  s_wlist	*dwlist = NULL;
  s_weapon	*w = NULL;
  s_mlist	*mtmp = g_mlist;
  s_mlist	*dmlist = NULL;
  s_monster	*m = NULL;

  while (wtmp != NULL)
  {
    w = wtmp->weapon;
    if (w->ally == 0 && collisionw(w))
    {
      g_player_1->health -= w->damage;
      dwlist = wtmp;
      wtmp = wtmp->next;
      //delete_weapon(g_wlist, wtmp);
    }
    else
      wtmp = wtmp->next;
  }

  while (mtmp != NULL)
  {
    m = mtmp->monster;
    if (collisionm(m))
    {
      g_player_1->health -= 50;
      dmlist = mtmp;
      if (dmlist->monster->health <= 500)
	delete_monster(dmlist);
      else
	dmlist->monster->health -= 100;
      mtmp = mtmp->next;
    }
    else
      mtmp = mtmp->next;
  }

}
